import ProTable from '@ant-design/pro-table';
import React, {useState} from 'react';
import {defaultPagination, paginationTool} from '@/utils/util';
import {useMount, useRequest} from 'ahooks';

import {getResult} from '@/services/ant-design-pro/attribute';
import {findByPageIntegralList} from '@/services/ant-design-pro/goods';
import {Link} from "@umijs/preset-dumi/lib/theme";

const columns = [
  {
    title: '产品编号',
    dataIndex: 'id',
    search: false,
  },
  {
    title: '产品名称',
    dataIndex: 'name',
  },
  {
    title: '产品分类',
    dataIndex: 'categoryName',
    search: false,
  },
  {
    title: '产品图片',
    dataIndex: 'image',
    valueType: 'image',
    search: false,
  },
  {
    title: '积分',
    dataIndex: 'integral',
    search: false,

  },
  {
    title: '库存',
    dataIndex: 'quantity',
    search: false,
  },
  {
    title: '时间',
    dataIndex: 'createDate',
    valueType: 'dateTime',
    hideInSearch: true,
  },
  {
    title: '状态',
    dataIndex: 'state',
    key: 'state',
    valueType: 'select',
    valueEnum: await getResult ( 'backsatgeloginstate' ),
  },
  {
    title: '操作',
    width: '164px',
    key: 'option',
    valueType: 'option',
    render: (_, row, index, action) => [

      <Link key="link" to={`/goods/show?id=${row.id}`}>
        详细
      </Link>,
    ],
  },
];

const cloneTbale = () => {
  const [pagination, setPagination] = useState<API.Pagination> ( defaultPagination () );
  const {
    run: fetchByPageList,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByPageIntegralList, {
    debounceWait: 100,
    manual: true,
  } );

  useMount ( () => {
    fetchByPageList ();
  } );

  return (
    <ProTable
      search={false}
      onDataSourceChange={(d) => {
        console.log ( 'onDataSourceChange', d );
      }}
      onLoad={(d) => {
        // 数据加载完操作
        console.log ( 'onDataSourceChange', d );
      }}
      toolBarRender={false}
      dataSource={dataList?.data?.list}
      loading={loading}
      pagination={paginationTool ( pagination, dataList?.data?.total, (data1: API.Pagination) => {
        setPagination ( data1 );

        fetchByPageList ( {...pagination} );
      } )}
      search={{
        defaultCollapsed: false,
        labelWidth: 'auto',
      }}
      cardBordered={true}
      columns={columns}
    ></ProTable>
  );
};

export default cloneTbale;
